package com.customer.controller.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.customer.entity.AICharacter;
import com.customer.result.Result;
import com.customer.service.AICharacterService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 推荐提问控制器
 */
@RestController
@RequestMapping("/user/ai/suggestions")
@Api(tags = "AI推荐提问相关接口")
public class AISuggestionController {

    @Autowired
    private AICharacterService aiCharacterService;

    /**
     * 获取角色相关的推荐提问
     */
    @GetMapping("/questions/{roleKey}")
    @ApiOperation("获取角色相关的推荐提问")
    public Result<List<String>> getSuggestedQuestions(@PathVariable String roleKey) {
        AICharacter character = aiCharacterService.getCharacterByRoleKey(roleKey);
        if (character == null) {
            return Result.error("角色不存在");
        }

        // 生成推荐提问
        List<String> questions = generateQuestionsForCharacter(character);
        return Result.success(questions);
    }

    private List<String> generateQuestionsForCharacter(AICharacter character) {
        List<String> questions = new ArrayList<>();
        String characterName = character.getCharacterName();
        String roleKey = character.getRoleKey();

        // 基础通用问题
        questions.addAll(Arrays.asList(
                "你好" + characterName + "，今天过得怎么样？",
                "能给我介绍一下你自己吗？",
                "你有什么特别的爱好吗？"
        ));

        // 根据角色类型生成特定问题
        if ("harry_potter".equals(roleKey)) {
            questions.addAll(Arrays.asList(
                    "在霍格沃茨的生活怎么样？",
                    "你最喜欢的科目是什么？",
                    "能给我讲讲魁地奇比赛的规则吗？"
            ));
        } else if ("wangye".equals(roleKey)) {
            questions.addAll(Arrays.asList(
                    "你如何理解\"道\"？",
                    "对于当前的生活，你有什么建议吗？",
                    "能分享一个你认为有哲理的故事吗？"
            ));
        } else if ("hatsune_miku".equals(roleKey)) {
            questions.addAll(Arrays.asList(
                    "你会唱哪些歌曲？",
                    "作为虚拟歌手，你觉得最有趣的事情是什么？",
                    "能教我一些音乐知识吗？"
            ));
        }

        // 随机选择4个问题返回
        Random random = new Random();
        return questions.stream()
                .sorted((a, b) -> random.nextInt(2) - 1)
                .limit(4)
                .collect(Collectors.toList());
    }
}